<template>
  <div>
    <p style="display: flex">
      <el-button type="info" icon="el-icon-refresh" size="mini"
                 @click="list()">刷新
      </el-button>
      <el-button type="info" size="mini"
                 @click="dialogVisible=true">添加
      </el-button>
    </p>
    <p style="display: flex">
      <el-pagination
          background
          layout="prev, pager, next"
          :page-size="pageInfo.size"
          :current-page="pageInfo.page"
          @current-change="list"
          :total="pageInfo.total">
      </el-pagination>
      <el-select v-model="pageInfo.size" placeholder="请选择"
                 @change="list(1)">
        <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"

            :value="item.value">
        </el-option>
      </el-select>
    </p>
    <div>
      <el-dialog title="添加角色" :visible.sync="dialogVisible">
        <el-form :model="form">

          <el-form-item label="角色" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>


          <el-form-item label="描述" :label-width="formLabelWidth">
            <el-input v-model="form.desc" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="权限" :label-width="formLabelWidth">
            <el-tree
                :data="allResource"
                show-checkbox
                node-key="id"
                ref="tree"
                highlight-current
                :props="defaultProps">
            </el-tree>
          </el-form-item>


        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible= false">取 消</el-button>
          <el-button type="primary" @click="saveRole">确 定</el-button>
        </div>
      </el-dialog>
      <el-table
          :data="pageInfo.list"
          style="width: 100%">
        <el-table-column
            prop="id"
            label="id"
            width="180">
        </el-table-column>
        <el-table-column
            prop="name"
            label="角色"
            width="180">
        </el-table-column>
        <el-table-column
            prop="desc"
            label="描述"
            width="180">
        </el-table-column>

        <el-table-column
            fixed="right"
            label="操作"
            width="100">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="edit(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

  </div>
</template>

<script>
import {listRole, deleteRole, saveRole, roleResource, saveRoleResource} from "@/network/role";
import {Confirm} from '@/assets/js/confirm'
import {Validator} from "@/assets/js/validator";
import {allResource} from "@/network/resource";

export default {
  name: "role",
  mounted() {
    this.list(1)
    this.loadResourceTree();
  },
  data() {
    return {
      pageInfo: {
        page: 1,
        size: 5,
        list: [],
        total: 0,
      }, options: [
        {
          value: 1,
          label: 1
        },
        {
          value: 5,
          label: 5
        },
        {
          value: 10,
          label: 10
        },
        {
          value: 50,
          label: 50
        },],
      dialogVisible: false,
      form: {},
      formLabelWidth: '120px',
      allResource: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
    }
  },
  methods: {
    list(page) {
      let _this = this;
      if (page !== null && page !== undefined) {
        _this.pageInfo.page = page;
      }
      _this.pageInfo.list = null;
      listRole(_this.pageInfo).then(res => {
        _this.pageInfo = res.data
      })
    },
    saveRole() {
      let _this = this;
      // 保存校验
      if (1 !== 1
          || !Validator.require(_this.form.name, "角色")
          || !Validator.length(_this.form.name, "角色", 1, 50)
          || !Validator.require(_this.form.desc, "描述")
          || !Validator.length(_this.form.desc, "描述", 1, 100)
      ) {
        return;
      }

      saveRole(_this.form).then(res => {
        _this.$message.success("保存成功");
        _this.form = {}
        _this.dialogVisible = false;
        _this.list()
      })
      let checkedKeys = this.$refs.tree.getCheckedKeys()

      saveRoleResource(_this.form.id, checkedKeys).then(res => {
        _this.$message.success("角色权限设置成功");
      })
      _this.dialogVisible = false;
    },
    edit(row) {
      console.log(row);
      let _this = this;
      _this.form = Object.assign({}, row)
      _this.dialogVisible = true;
      roleResource(row.id).then(res => {
        let checked = [];
        for (let i = 0; i < res.data.length; i++) {
          checked.push(res.data[i].resourceId);
        }
        _this.$refs.tree.setCheckedKeys(checked);
      })
    },
    del(row) {
      let _this = this;
      Confirm.show('此操作将永久删除该文件, 是否继续?', function () {
        deleteRole(row.id).then(res => {
          _this.list();
          _this.$message({
            type: 'success',
            message: '删除成功!'
          });
        })
      })

    },
    loadResourceTree() {
      let _this = this;
      allResource().then(res => {
        _this.allResource = res.data;
      })
    }
  }
}
</script>

<style scoped>

</style>
